-- radius_trap
-- create by zhuangsl
-- 距离型陷阱

return {
    getTargets = function(trapId, pos, targetPos)
        local info = CommonTrapM.query(trapId);
        local trapType = info["type"];
        local feature = info["feature"];

        assert(trapType == TRAP_TYPE_RADIUS,
            "不是范围型或全局型的陷阱！");

        local radius = info["radius"];
        local maxRadius = math.max(DUNGEON_HEIGHT, DUNGEON_WIDTH);
        if type(radius) ~= "number" or
            radius == NUMBER_INFINITY or
            radius > maxRadius then
            radius = maxRadius;
        end

        local ret = {};
        local coor = DungeonM.convertToCoor(pos);
        local x = coor["x"];
        local y = coor["y"];

        for index = 1, GRID_SIZE do
            repeat
                if index == pos then
                    break;
                end

                local c = DungeonM.convertToCoor(index);
                local xx = c["x"];
                local yy = c["y"];
                if math.abs(xx - x) + math.abs(yy - y) > radius then
                    -- 超出范围了
                    break;
                end

                local grid = DungeonM.getGridByPos(index);
                local monster = grid.monster;

                if bit.band(feature, TRAP_FEATURE_IGNORE_BLOCK) ~= TRAP_FEATURE_IGNORE_BLOCK then
                    if not grid:isOpened() then
                        break;
                    else
                        if  not grid:isMonster() or
                            not monster or
                            monster:isDead() then
                            break;
                        end
                    end
                end

                table.insert(ret, grid);
            until true;
        end

        return ret;
    end,
};
